<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/07/3
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Driver;
use think\Db;
use think\Config;
/**
 * 司机VIP🟥
 * 
 */
class DriverVip extends Driver
{

    protected $noNeedLogin = ['p'];
    protected $noNeedRight = ['*'];
    protected $db = 'jvip_order';

    public function _initialize()
    {
        parent::_initialize();
        
        // $user = $this->auth->getUserinfo();

        // if($user->group_id==1){
        //     $this->error('普通用户！非司机');
        // }
        
    }


    /**
     * 购买vip
     * 
     * @ApiSummary ("code1=微信支付_2支付宝_3钱包支付成功,-3钱包余额不足")
     * @param int $pay_type 1支付类型:1=微信,2=支付宝,3=钱包,9=其他
     * @param int $num 数量（年） 1
     * @return void
     */
    public function buy_vip($num=1,$pay_type = '3')
    {
        $data['money'] = Config::get('site.j_vip_year_price')*$num;
        $data['num']  = $num;
        $data['uid'] = $this->auth->id;
        $data['pay_status'] = '1';//支付状态:1=待支付,2=已支付,3=已退款,4=已超时
        $data['pay_type']  = $pay_type;//支付类型:1=微信,2=支付宝,3=钱包,9=其他
        $data['get_status'] = '1';//获取方式:1=购买,2=领取

        $res = Db::name($this->db)->where($data)->find();
        if($res){
            $data = $res;
        }else{
            $data['order'] = $this->rand_order('JVip1'.$pay_type,$this->auth->id);



            $data['time'] = time();
            // $data['ps'] = $ps;
            $data['id'] = Db::name($this->db)->insertGetId($data);
        }


        //支付
        $Pay = new \app\api\controller\Pay();
        switch ($pay_type) {
            case '1':
                $res = $Pay->wx_pay_jvip_order($data);
                $status_code = 1;
                break;
            case '2':
                $res = $Pay->ali_pay_jvip_order($data);
                $status_code = 2;
                break;
            case '3':
                $this->MoneyPay($this->auth->id,$data['money'],'开通司机Vip');
                //钱扣了修改订单状态
                $vip_order['pay_status'] = '2';//支付状态:1=待支付,2=已支付,3=已退款,4=已超时
                $vip_order['pay_time'] = time();//支付时间
                $vip_order['pay_type'] = '3';    //支付类型:1=微信,2=支付宝,3=钱包,9=其他
                $vip_order['pay_money'] = intval($data['money']*100);//实际支付的金额 分

  
    
                $res_up = Db::name($this->db)
                        ->where('order',$data['order'])
                        ->update($vip_order);
                
                
                $this->AddUserJVipEtimeByOrder($data['order']);//增加用户的vip 时间 购买订单
                $status_code = 3;
                $this->success('钱包支付成功',$vip_order,$status_code);
                break;
            default:
                $this->error('暂未开通此项支付');
                break;
        }

    
        $this->success('ok',$res,$status_code);
    }


    /**
     * 领取vip
     * @param int $ps 备注
     * @return void
     */
    public function get_vip($ps='领取vip',$pay_type = '9')
    {
        $data['money'] = 0;//Config::get('site.j_vip_year_price')*$num;
        $data['num']  = 1;
        $data['uid'] = $this->auth->id;
        $data['pay_status'] = '2';//支付状态:1=待支付,2=已支付,3=已退款,4=已超时
        $data['pay_type']  = $pay_type;//支付类型:1=微信,2=支付宝,9=其他
        $data['get_status'] = '2';//获取方式:1=购买,2=领取
        $data['pay_money'] = 0;

        $res = Db::name($this->db)->where($data)->find();
        if($res){
            $data = $res;
            $this->error('已领取过');
        }else{
            $data['order'] = $this->rand_order('JVip2'.$pay_type,$this->auth->id);



            $data['time'] = time();
            $data['ps'] = $ps;
            $data['id'] = Db::name($this->db)->insertGetId($data);
            $this->AddUserJVipEtimeByOrder($data['order']);
            $this->success('领取成功');
        }


   

        
    }



    /**
     * 获取我的充值vip订单列表
     *
     * 
     * @param string $pay_status 不传为全部，支付状态:1=待支付,2=已支付,3=已退款,4=已超时
     * @param integer $page 1
     * @param integer $limit 10

     * @return void
     */
    public function get_my_vip_order_list($page=1,$limit=10,$pay_status=null)
    {
        $where['uid'] = $this->auth->id;

        $list = Db::name($this->db)->where($where)->page($page,$limit)->select();
        $this->success('ok',$list);
    }


    /**
     * 获取vip订单信息
     *
     * @param int $id VIP订单信息id
     * @return void
     */
    public function get_order_data($id)
    {
        $where['uid'] = $this->auth->id;
        $where['id'] = $id;
        $data = Db::name($this->db)->where($where)->find();
        $this->success('ok', $data);
    }

}